<?php
	
	include_once('../config.php'); 
	LinkDataBase();
	
	?>
	<script type="text/javascript">
	<?php
	//Find Category.
	$sql = "SELECT * FROM droplist WHERE year='".$_POST['year']."' AND season='".$_POST['season']."'";
	$result = mysql_query($sql);
	
	$index = 0;
	while($row = mysql_fetch_assoc($result)) {
		
		//Find table number in category.
		$sql = "SELECT table_name, sheet.table_number AS table_number, charttype, keyword, contain 
		FROM sheetreveal_first 
		LEFT JOIN sheet ON sheet.table_number=sheetreveal_first.table_number 
		WHERE droplist_id = '".$row['id']."' AND sheet.season = '".$_POST['season']."'";
		
		$result_sheetreveal_first = mysql_query($sql);
		
		$index_detail = 0;
		if ($result_sheetreveal_first) {
			while($row_first = mysql_fetch_assoc($result_sheetreveal_first)) {
				
				$table_number = $row_first['table_number'];
				
				//Check category contain sub-category.
				if ($row['sub_name']==NULL) {
					
					$sheetreveal_first[$row['name']]['table_number'][] = $table_number;
					$sheetreveal_first[$row['name']]['table_name'][] = $row_first['table_name'];
					
					$hrefID = 'ac-'.$index;
				}
				else {
					
					$Title_ContainSub[$row['name']][$row['sub_name']]['table_number'][] = $table_number;
					$Title_ContainSub[$row['name']][$row['sub_name']]['table_name'][] = $row_first['table_name'];
					
					$hrefID = 'sub-ac-'.$index;
				}
				?>
					
				var href = '<?php echo $hrefID; ?>';
				var ChartType = '#<?php echo $row_first['charttype']; ?>';
				
				//Process keyword.
				var keyword = '';
				<?php
				if (isset($row_first['keyword'])) {
				?>
					keyword = '<?php echo $row_first['keyword']; ?>';
				<?php
				}
				?>
						
				//Process contain.
				var contain = '';
				<?php
				if (isset($row_first['contain'])) {
				?>
					contain = '<?php echo $row_first['contain']; ?>';
				<?php
				}
				?>
				//Change href.
				$('.ac-container article#'+href+' a#href-<?php echo $index_detail; ?>').attr('href','#<?php echo $table_number; ?>'+ChartType+'#'+keyword+'#'+contain);
				<?php
				$index_detail++;
			}
		}
		$index++;
	}
?>
	function GotoDetail(href, title) {
					
		var year = $("#year-droplist").text();
		var season = $("#season-droplist").text();
		var type = $("#type-droplist").text();
		var Array_Href = $(href).attr('href').split('#');
		$("#content-detail").load('../chart/detail/index.php',
		{'year':year, 'season':season, 'type':type, 'tablename': Array_Href[1], 'charttype': Array_Href[2], 
		'keyword': Array_Href[3], 'contain': Array_Href[4], 'step': 1});
		
		console.log(Array_Href);
	}
</script>
<div class="container">
			<header>
				<h1>TABLE OF CONTENTS</h1>
			</header>
            <section class="ac-container">
            
            <?php 
			$index = 0;
			//Search first stage.
			while ((is_array($sheetreveal_first))&&(current($sheetreveal_first))) {
				
				$Title = key($sheetreveal_first);
			?>
            	<div>
					<input id="ac-<?php echo $index; ?>" name="accordion-<?php echo $index; ?>" type="checkbox"/>
					<label for="ac-<?php echo $index; ?>"><?php echo $Title; ?></label>
                
					<article class="ac-small" id="ac-<?php echo $index; ?>">
                    <ul>
                    <?php
					
					//Search second stage.
					for ($index_detail=0;$index_detail<count($sheetreveal_first[$Title]['table_number']);$index_detail++) {
					?>
                    	<a id="href-<?php echo $index_detail; ?>" href="#" onclick="GotoDetail(this);">
                        	<li>
								<?php echo $sheetreveal_first[$Title]['table_name'][$index_detail]; ?>
                        	</li>
                        </a>
                    <?php
					}
					?>
                    </ul>
                    </article>
                </div>
            <?php
				next($sheetreveal_first);
				$index++;
			}
			?>
            
            </section>
            
            <?php
			//For contain sub-category.
			//Search first stage.
			while ((is_array($sheetreveal_first))&&(current($Title_ContainSub))) {
				
				$Title = key($Title_ContainSub);
			?>
                <header>
                	<h2><?php echo $Title; ?></h2>
				</header>
                <section class="ac-container">
                	
                    <?php
					//Search second stage.
					for ($sub_index=0;$sub_index<count($Title_ContainSub[$Title]);$sub_index++) {
						$Array_Key = array_keys($Title_ContainSub[$Title]);
						$id_index = $sub_index+$index;
					?>
                        <div>
                            <input id="sub-ac-<?php echo $id_index; ?>" name="sub-accordion-<?php echo $id_index; ?>" type="checkbox"  />
                            <label for="sub-ac-<?php echo $id_index; ?>"><?php echo $Array_Key[$sub_index]; ?></label>
                            <article class="ac-small" id="sub-ac-<?php echo $id_index; ?>">
                                <ul>
                                <?php 
								
								//Search third stage.
								for ($index_detail=0;$index_detail<count($Title_ContainSub[$Title][$Array_Key[$sub_index]]['table_number']);$index_detail++) {
								?>
									<a id="href-<?php echo $index_detail; ?>" href="#" onclick="GotoDetail(this);">
                                    	<li>
											<?php echo $Title_ContainSub[$Title][$Array_Key[$sub_index]]['table_name'][$index_detail]; ?>
                                    	</li>
                                    </a>
								<?php
								}
								?>
                                </ul>
                            </article>
                        </div>
					<?php	
					}
					?>                
                
                </section>
            <?php
				next($Title_ContainSub);
			}
			?>
</div>
